export function setupUploader(element) {
    const setUploader = () => {
        element.innerHTML = `上传文件`
    }
    element.addEventListener('click', () => {
        const input = document.createElement('input');
        input.type = 'file';
        input.style.display = 'none';

        input.addEventListener('change', async () => {
            const file = input.files[0];
            if (!file) return;

            // 构建 FormData 上传
            const formData = new FormData();
            formData.append('file', file, file.name);

            try {
                const res = await fetch('/upload', {
                    method: 'POST',
                    body: formData,
                });
                const result = await res.json();
                console.log('上传成功:', result);
            } catch (err) {
                console.error('上传失败:', err);
            }
        });

        // 触发文件选择对话框
        document.body.appendChild(input);
        input.click();
        document.body.removeChild(input);
    });
    setUploader()
}
